package com.wt.wechatTools;

import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import com.wt.wechatTools.databinding.FragmentHomeBinding;
import com.wt.wechatTools.room.WorkRepository;
import com.wt.wechatTools.room.entity.UserEntity;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class FragmentHome extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        BottomNavigationView bottomNavigationView = getActivity().findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setVisibility(View.VISIBLE);

        FragmentHomeBinding binding;
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);

        FragmentHomeAsyncTack fragmentHomeAsyncTack = new FragmentHomeAsyncTack(binding);
        fragmentHomeAsyncTack.execute();

        return binding.getRoot();
    }

    private class FragmentHomeAsyncTack extends AsyncTask<Void,Void,UserEntity> {
        private WorkRepository workRepository;
        private UserEntity userEntity;
        private FragmentHomeBinding binding;
        private String basTime = "2001-01-01 01:01";
        private AlertDialog alertDialog;

        public FragmentHomeAsyncTack(FragmentHomeBinding binding) {
            this.binding = binding;
            this.workRepository = new WorkRepository(getContext());
        }

        @Override
        protected UserEntity doInBackground(Void... voids) {
            userEntity = workRepository.getUser();
            if (userEntity == null) {
                workRepository.insertUsers(new UserEntity(null, "", "", "", 10, 0, 2000,  basTime,"",1));
                userEntity = workRepository.getUser();
            }
            return userEntity;
        }


        @Override
        protected void onPostExecute(UserEntity userEntity) {
            super.onPostExecute(userEntity);
            binding.txtUserName.setText("用户名：" + (userEntity.getUserName().isEmpty() ? "" : userEntity.getUserName()));

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            try {
                Date endTime = dateFormat.parse(userEntity.getEndTime());
                Date baseTime = dateFormat.parse(basTime);
                if (endTime.getTime() == baseTime.getTime()) {
                    binding.txtEndTime.setText("过期日期：已过期");
                } else {
                    binding.txtEndTime.setText("过期日期：" + userEntity.getEndTime());
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

            //准备提示框
            alertDialog = new AlertDialog.Builder(getContext()).create();
            alertDialog.setTitle("提示");
            alertDialog.setMessage("您的套餐已过期，点击确认前往充值！");
            alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //跳转至充值界面
                    NavController nv = Navigation.findNavController(getView());
                    nv.navigate(R.id.fragmentCdk);
                }
            });

            alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

            /**
             * 启动引导
             */
            binding.btnAddFriend.setOnClickListener(new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.M)
                @Override
                public void onClick(View view) {
                    //判断用户当前剩余次数是否足够
                    String endTimeString = userEntity.getEndTime();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    Date nowDate = new Date();
                    try {
                        Date endTime = sdf.parse(endTimeString);
                        if (endTime.getTime() > nowDate.getTime()) {
                            //跳转至启动引导界面
                            NavController nv = Navigation.findNavController(view);
                            nv.navigate(R.id.action_fragmentHome_to_fragmentTaskFlow);
                        } else {
                            alertDialog.show();
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            });



        }
    }

}

